/*
 * SyncResource.java
 *
 * Created on April 12, 2007, 1:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.admin;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.UUID;
import java.util.logging.Level;
import org.atomojo.app.App;
import org.atomojo.app.AtomResource;
import org.atomojo.app.db.DB;
import org.atomojo.app.db.Entry;
import org.atomojo.app.db.EntryMedia;
import org.atomojo.app.db.Feed;
import org.atomojo.app.db.Journal;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.OutputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class JournalResource extends ServerResource
{
   
   /** Creates a new instance of SyncResource */
   public JournalResource() {
      setNegotiated(false);
   }
   
   public Representation get()
   {
      final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
      return new OutputRepresentation(MediaType.APPLICATION_XML) {
         public void write(OutputStream os)
            throws IOException
         {
            try {
               Writer out = new OutputStreamWriter(os,"UTF-8");
               out.write("<journal>\n");
               Iterator<Journal.Entry> entries = db.getJournal().getDeletes();
               while (entries.hasNext()) {
                  Journal.DeleteEntry delete = (Journal.DeleteEntry)entries.next();
                  out.write("<delete at='");
                  out.write(AtomResource.toXSDDate(delete.getOccurredOn()));
                  out.write("' feed='");
                  out.write(delete.getFeed().toString());
                  out.write("' path='");
                  out.write(delete.getPath());
                  UUID entryId = delete.getEntry();
                  if (entryId!=null) {
                     out.write("' entry='");
                     out.write(entryId.toString());
                  }
                  out.write("'/>\n");
               }
               entries = db.getJournal().getUpdates();
               while (entries.hasNext()) {
                  Journal.UpdatedEntry updated = (Journal.UpdatedEntry)entries.next();
                  String name = updated.getOperation()==Journal.CREATE_OPERATION ? "created" : "updated";
                  Feed feed = updated.getFeed();
                  Entry entry = updated.getEntry();
                  EntryMedia media = updated.getResource();
                  out.write('<');
                  out.write(name);
                  out.write(" at='");
                  out.write(AtomResource.toXSDDate(updated.getOccurredOn()));
                  out.write("' feed='");
                  out.write(feed.getUUID().toString());
                  if (entry!=null) {
                     out.write("' entry='");
                     out.write(entry.getUUID().toString());
                  }
                  if (media!=null) {
                     out.write("' media='");
                     out.write(media.getName());
                  }
                  out.write("'/>\n");
               }
               out.write("</journal>");
               out.flush();
            } catch (SQLException ex) {
               getContext().getLogger().log(Level.SEVERE,"Cannot get journal entries due to SQL exception.",ex);
               throw new IOException("Cannot get journal entries due to SQL exception: "+ex.getMessage());
            }
         }
      };
   }
   
   public Representation delete() {
      final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
      try {
         db.getJournal().truncate();
         getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
         return null;
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get journal entries due to SQL exception.",ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Truncate failed due to database excpetion.");
      }
   }
   
}
